<%--
  Created by IntelliJ IDEA.
  User: 风待葬
  Date: 2020/6/11
  Time: 16:19
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>角色列表页面</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<%--头部多条件查询--%>
<form class="layui-form" >
    <div class="layui-inline">
        <label class="layui-form-label">角色名</label>
        <div class="layui-input-inline">
            <input type="text" name="rolename" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-inline">
            <select name="status" >
                <option value="">请选择</option>
                <option value="1">正常</option>
                <option value="0">注销</option>
            </select>
        </div>
    </div>
    <div class="layui-inline">
        <div class="layui-input-block">
            <button class="layui-btn" lay-filter="query"  lay-submit>查询</button>
        </div>
    </div>
</form>
<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="add">新增</button>
        <button class="layui-btn layui-btn-sm" lay-event="delete">删除</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script src="${pageContext.request.contextPath}/static/layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.use(['table','form','layer','jquery'], function(){
        var table = layui.table;
        var layer=layui.layer,
            form=layui.form;
        var $=layui.$;
        form.on('submit(query)',function (data) {
            console.log(data.elem)
            console.log(data.form)
            console.log(data.field)
            table.reload("test",{
                where:{
                    params:data.field
                },
                page:{
                    curr:1
                }
            })
            return false;//阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        table.render({
            elem: '#test'
            ,url:'${pageContext.request.contextPath}/role/laylist'
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]
            ,title: '用户数据表'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
                ,{field:'rolename', title:'角色名', width:120, edit: 'text'}
                ,{field:'explain', title:'描述', width:150, edit: 'text',}
                ,{field:'status', title:'状态', width:80,templet: function(res) {//res当前行数据
                        if (res.status == "1") {
                            return "正常";
                        }
                        return "注销";
                    }}
                ,{field:'operator', title:'操作人', width:100}
                ,{field:'createtime', title:'创建时间', width:120}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]]
            ,page: true
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
                case 'add':
                    //layer.msg('添加');
                    layer.open({
                        title:"角色新增",
                        type:2,//为iframe层，可以加载其他的页面到弹出层中
                        content:"${pageContext.request.contextPath}/role/toAdd",
                        area:['500px','400px'],//定义宽高
                        maxmin:true//可以最大化
                    })
                    break;
                case 'delete':
                    layer.msg('删除');
                    console.log(checkStatus)
                    console.log(checkStatus.data)//选中的数据
                    if(checkStatus.data.length==0){
                        layer.alert("请至少选择一项删除！");
                        return;
                    }
                    var data=checkStatus.data;
                    var idArray=[];
                    //获取ID值,并放入数组
                    for(var i=0;i<data.length;i++){
                        idArray.push(data[i].id);
                    }
                    console.log(idArray);
                    //执行批量删除
                    $.post("${pageContext.request.contextPath}/role/batchDelete",
                        {ids:idArray.join(",")},function (result) {
                            if(result.code==0){
                                layer.msg("删除成功");
                                //重写加载表格
                                table.reload("test",{
                                    page:{
                                        curr:1
                                    }
                                })
                                return
                            }
                            layer.msg("删除失败");
                        },"json")
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                layer.open({
                    title:"角色修改",
                    type:2,//为iframe层，可以加载其他的页面到弹出层中
                    content:"${pageContext.request.contextPath}/role/toUpdate?id="+data.id+"",
                    area:['600px','500px'],//定义宽高
                    maxmin:true//可以最大化
            })
            }
        });
    });
</script>

</body>
</html>
